package fzdy.controller;

import com.alibaba.fastjson.JSON;
import fzdy.entity.Sysmodule;
import fzdy.service.IRoleService;
import fzdy.service.ISysmoduleService;
import fzdy.entity.Role;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2018/1/8 0008.
 */
@Controller
@RequestMapping(value = "/sysmanage/moudle")
public class MouduleController {

    @Autowired
    private ISysmoduleService sysmoduleService;
    @Autowired
    private IRoleService roleService;

    @RequestMapping(value = "/login")
    @ResponseBody
    public String login(@RequestBody Role role) {
        List<Role> roles = roleService.getList();
        Integer roleId = roles.get(0).getId();
        List<Sysmodule> sysmodules = sysmoduleService.getusermenudata(roleId);
        String s = JSON.toJSONString(getUserMenu(sysmodules));
        return s;
    }

    private List getUserMenu(List<Sysmodule> sysmodules) {
        List<Sysmodule> list=new ArrayList<>();
        for (Sysmodule sysmodule : sysmodules) {
            if (sysmodule.getParentId() == null) {
                list.add(sysmodule);
            }
        }
        for (Sysmodule sysmodule : sysmodules){
            if (sysmodule.getParentId() != null) {
                list.get(sysmodule.getParentId()-1).getChildren().add(sysmodule);
            }
        }
        return list;
    }

}
